package zj.com.activities;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;


import java.io.File;

import zj.com.model.UserInfo;
import zj.com.util.CommonUtil;
import zj.com.util.LogUtils;
import zj.com.util.SharedPreferencesUtil;
import zj.com.zj.R;

public class SplashActivity extends BaseActivity {


    public final int ENTER_HOME = 0x2210;
    public final int ENTER_GUIDE = ENTER_HOME+1;
    public final  int LOGIN = ENTER_HOME+2;
    public Intent intent;
    SharedPreferences mySharedPreferences;
    SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);


        mySharedPreferences= getSharedPreferences("zj",
                Activity.MODE_PRIVATE);


        int first = mySharedPreferences.getInt("first", 0);
        Message msg = handler.obtainMessage();

        if(first == 1){
            //UserInfo uinfo = (UserInfo) SharedPreferencesUtil.readObject(this, "user");
           String token = (String) SharedPreferencesUtil.getData(this,"token","");

            if(!TextUtils.isEmpty(token)){
                LogUtils.getInstance().error("ENTER_HOME==");

                msg.what = ENTER_HOME;

            }else{
                msg.what = LOGIN;

            }

        }else{
            CommonUtil.deleteDir(new File("/sdcard/zj/"));//删除数据库里面文件
            msg.what = ENTER_GUIDE;
        }

        handler.sendMessageDelayed(msg,2000);

    }


   Handler handler = new Handler(){


       @Override
       public void handleMessage(Message msg) {
           switch (msg.what){
               case ENTER_HOME:
                   intent = new Intent(SplashActivity.this,MainTabActivity.class);
                   startActivity(intent);
                   finish();
                   break;
               case ENTER_GUIDE:
                   intent = new Intent(SplashActivity.this,GuideActivity.class);
                   startActivity(intent);
                   finish();
                   break;
               case LOGIN:
                   intent = new Intent(SplashActivity.this,LoginActivity.class);
                   startActivity(intent);
                   finish();;
                   break;
           }
           super.handleMessage(msg);
       }
   };


}
